class Page{
			constructor(options){
				this.list = options.list;
				this.url = options.url;
				this.box = options.box;
				
				this.load()
				this.addEvent()
			}
			
			load(){
				var that = this
				$.ajax({
					url:this.url,
					type:"get",
					data:{
						dataName:'follow',
						rule:'time',
						start:1,
						count:100
					},
					success:function(res){
						console.log(res);
						that.res = res;
						that.display()
					}
				})
				console.log(this.box=this.list)
			}
			
			display(){
				var str = ""
				for(var i = this.res.page_data.length-1;i>=0;i--){
//					if(i<this.res.length){
					str +=`<li index="${this.res.page_data[i]._id}">
						<div class="sp">
							<img src="${this.res.page_data[i].detail.auth_icon}"/>
							<span class="price">¥${this.res.page_data[i].des}</span>
							<p class="msg">${this.res.page_data[i].title}</p>
						</div>
							<div class="join">加入购物车</div>
						</li>`
//					}
				}
				this.list.html(str)
				// console.log(this.res[0][0].id)
			}
			
			addEvent(){
				var that = this;
				this.list.on("click",".join",function(evt){
//					that.id= $(evt.target).parent().attr("index")
					that.id= $(this).parent().attr("index")
					
					that.setGoods()
				})
				
				
//				this.list.not.on("")
				this.list.on("click",".sp",function(){
					console.log($(this).parent().attr("index"))
					var a = $(this).parent().attr("index")
					var b = $(this).parent('li').parent('ul').attr("dataName")
					// console.log(b)
					var c = JSON.stringify({"index":a,"dataName":b})

					that.setgoodsdetail(c)
//					location.href="http://localhost/二升三项目/goodsdetail.html"
				})
				
			}
			
			setgoodsdetail(c){
				// console.log(a)
				$.cookie("goodsdetail",c)
				location.href="goodsdetail.html"
				
			}
			
			setGoods(){
//				console.log(111)
    	
//  	this.goods = $.cookie("goods")==="" ? [] : JSON.parse($.cookie("goods"))
    	if($.cookie("goods")== null){
    		this.goods=[]
    	}else{
    		this.goods= JSON.parse($.cookie("goods"))
    	}
				console.log(this.goods)
				if(this.goods.length<1){
					this.goods.push({
						id:this.id,
						num:1
					})
				}else{
					var onoff = true;
					for(var i = 0;i<this.goods.length;i++){
						if(this.goods[i].id == this.id){
							this.goods[i].num++;
							onoff = false;
							break;
						}
					}
					if(onoff){
						this.goods.push({
							id:this.id,
							num:1
						})
					}
				}
				$.cookie("goods",JSON.stringify(this.goods))
			}
			
		}

		


		new Page({
			list:$(".goodslist"),
			box:$(".goodslist").not(".join"),
			
			url:"api/product",
		})
//		
var socket = io(port)

socket.on('update_product',(data)=>{
	console.log('收到',data)
	var sendstr = $('.goodslist').html()+`<li index="${data.data._id}">
	<div class="sp">
		<img src="${data.data.detail.auth_icon}"/>
		<span class="price">¥${data.data.des}</span>
		<p class="msg">${data.data.title}</p>
	</div>
		<div class="join">加入购物车</div>
	</li>`
	$('.goodslist').html(sendstr)
})